/*
 * Copyright (C) 2004, Velocityme Partnership
 * UserAccessActions.java
 *
 * Created on 15 September 2004, 08:55
 */

package com.velocityme.www.interfaces;

import com.velocityme.session.KeyLocal;
import com.velocityme.session.UserAccessLocal;
import com.velocityme.www.actionforms.UserAccessActionForm;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author  Robert
 */
public class UserAccessActions extends DispatchAction {
    
    /** Creates a new instance of UserAccessActions */
    public UserAccessActions() {
    }
    
    public ActionForward edit(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        UserAccessLocal userAccessLocal = lookupUserAccessBean();
        Long nodeId = (Long)session.getAttribute("nodeId");
        if (request.getParameter("btnSubmit").equals("OK")) {
            Long userId = ((UserAccessActionForm)form).getUserId();
            Long roleId = ((UserAccessActionForm)form).getRoleId();
            userAccessLocal.createUserAccess(key, nodeId, userId, roleId);
        }
        // Cancel simply displays the original node again
        else {
        }
        request.setAttribute("nodeId", nodeId);
        return mapping.findForward("ACTION_SUCCESS");
    }
    
    public ActionForward deleteCheck(ActionMapping mapping, 
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        session.setAttribute("userAccessValue", form);
        return mapping.findForward("ACTION_DELETE");
    }
    
    public ActionForward delete(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        if (request.getParameter("btnSubmit").equals("OK")) {
            KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
            UserAccessLocal userAccessLocal = lookupUserAccessBean();
            Long userAccessId = ((UserAccessActionForm)form).getAccessId();
            userAccessLocal.deleteUserAccess(key, userAccessId);
        }
        request.setAttribute("nodeId", session.getAttribute("nodeId"));
        return mapping.findForward("ACTION_SUCCESS");
    }

    private UserAccessLocal lookupUserAccessBean() {
        try {
            Context c = new InitialContext();
            return (UserAccessLocal) c.lookup("java:comp/env/UserAccessBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
    
}
